//
//  RCMerchantUploadPhotoViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-30.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantUploadPhotoViewController.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "NCCameraManager.h"
#import "UIImageView+CutToResize.h"
#import "RCMerchantPhotoUploadDataModel.h"
#import "RCMerchantUploadPhotoTypeView.h"
#import "IManager+ModuleMerchantPictureUpload.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "RCMerchantDishesViewController.h"
#import "NSString+NSString_Ex_.h"
#import "RCFeedbackDataModel.h"


#define kGetPicFromAlbumString		          @"从相册中选取"
#define kGetPicFromCameraString		          @"拍照"
#define kDeletePicString                      @"删除照片"
#define kCancelString				          @"取消"


#define kLeftButtonImageFile       @"image_merchant_commit_wrong_leftButton.png"
#define kRightButtonImageFile    @"image_merchant_commit_wrong_rightButton.png"

#define kCenterImageFile            @"image_merchant_addImage_center.png"
#define kGreyImageFile              @"image_merchant_addImage_grey.png"
#define kOrangeImageFile            @"image_merchant_addImage_orange.png"
#define kDeleteImageFile            @"image_merchant_addImage_delete.png"

#define kFormImageFile           @"image_about_textBG.png"
#define kLineImageFile           @"image_line_type3.png"

#define kOrderForm1ImageFile     @"image_activity_order_form1.png"
#define kOrderForm2ImageFile     @"image_activity_order_form2.png"
#define kOrderForm3ImageFile     @"image_activity_order_form3.png"

@interface RCMerchantUploadPhotoViewController ()<NCCameraManagerDelegate,UITextFieldDelegate,IManagerDelegate,MBProgressHUDDelegate,RCUploadPhotoTypeViewDelegate,RCMerchantDishedDelegate>

@property(nonatomic,copy) NSString *merchantID;
@property(nonatomic,copy) NSString *merchantName;
@property(nonatomic,copy) NSString *typeID;
@property(nonatomic,copy) NSString *typeParentID;
@property(nonatomic,retain)UIButton *leftButton;
@property(nonatomic,retain)UIButton *rightButton;

@property(nonatomic,retain)UIImageView *leftGreyImageView;
@property(nonatomic,retain)UIImageView *leftCenterImageView;
@property(nonatomic,retain)UIImageView *leftImageView;
@property(nonatomic,copy)  UIImage     *leftImage;
@property(nonatomic,retain)UIImageView *leftOrangeImageView;
@property(nonatomic,retain)UIButton    *leftDeleteImageView;
@property(nonatomic,retain)UIButton    *leftTransparentImageView;

@property(nonatomic,retain)UIImageView *rightGreyImageView;
@property(nonatomic,retain)UIImageView *rightCenterImageView;
@property(nonatomic,retain)UIImageView *rightImageView;
@property(nonatomic,copy)  UIImage     *rightImage;
@property(nonatomic,retain)UIImageView *rightOrangeImageView;
@property(nonatomic,retain)UIButton    *rightDeleteImageView;
@property(nonatomic,retain)UIButton    *rightTransparentImageView;

@property(nonatomic,assign)BOOL        isLeftButtonClicked;
@property(nonatomic,retain)NCCameraManager *cameraManager;
@property(nonatomic,retain)RCMerchantPhotoUploadDataModel *typeDataModel;
@property(nonatomic,retain)RCMerchantUploadPhotoTypeView *typeView;

@property(nonatomic,retain)UIButton *typeButton;
@property(nonatomic,retain)UILabel *typeLabel;
@property(nonatomic,retain)UIImageView *titleImageView;
@property(nonatomic,retain)UITextField *titleTextField;
@property(nonatomic,retain)UIButton *dishNameButton;

@property(nonatomic,assign)int64_t typeRequestID;
@property(nonatomic,assign)int64_t uploadRequestID;
@property(nonatomic,retain)MBProgressHUDEx *progressHub;

@end

@implementation RCMerchantUploadPhotoViewController

@synthesize merchantID = _merchantID;
@synthesize merchantName = _merchantName;
@synthesize typeID = _typeID;
@synthesize typeParentID = _typeParentID;
@synthesize leftButton = _leftButton;
@synthesize rightButton = _rightButton;
@synthesize typeRequestID = _typeRequestID;
@synthesize uploadRequestID = _uploadRequestID;

@synthesize leftGreyImageView = _leftGreyImageView;
@synthesize leftCenterImageView = _leftCenterImageView;
@synthesize leftImageView = _leftImageView;
@synthesize leftImage = _leftImage;
@synthesize leftOrangeImageView = _leftOrangeImageView;
@synthesize leftDeleteImageView = _leftDeleteImageView;
@synthesize leftTransparentImageView = _leftTransparentImageView;

@synthesize rightGreyImageView = _rightGreyImageView;
@synthesize rightCenterImageView = _rightCenterImageView;
@synthesize rightImageView = _rightImageView;
@synthesize rightImage = _rightImage;
@synthesize rightOrangeImageView = _rightOrangeImageView;
@synthesize rightDeleteImageView = _rightDeleteImageView;
@synthesize rightTransparentImageView = _rightTransparentImageView;
@synthesize isLeftButtonClicked = _isLeftButtonClicked;
@synthesize cameraManager = _cameraManager;
@synthesize typeDataModel = _typeDataModel;
@synthesize typeView = _typeView;

@synthesize typeButton = _typeButton;
@synthesize typeLabel = _typeLabel;
@synthesize titleImageView = _titleImageView;
@synthesize titleTextField = _titleTextField;
@synthesize dishNameButton = _dishNameButton;
@synthesize progressHub = _progressHub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithShopID:(NSString *)shopID;{
    self = [super init];
    if (self) {
        self.merchantID = shopID;
    }
    return self;
}

-(void)dealloc{
    
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_typeRequestID];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_uploadRequestID];
    
    self.merchantID = nil;
    self.merchantName = nil;
    self.typeID = nil;
    self.typeParentID = nil;
    self.leftButton = nil;
    self.rightButton = nil;
    
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    
    self.cameraManager.delegate = nil;
    self.cameraManager = nil;
    self.typeDataModel = nil;
    self.typeView = nil;
    
    self.leftGreyImageView = nil;
    self.leftCenterImageView = nil;
    self.leftImageView = nil;
    self.leftImage = nil;
    self.leftOrangeImageView = nil;
    self.leftDeleteImageView = nil;
    self.leftTransparentImageView = nil;
    
    self.rightGreyImageView = nil;
    self.rightCenterImageView = nil;
    self.rightImageView = nil;
    self.rightImage = nil;
    self.rightOrangeImageView = nil;
    self.rightDeleteImageView = nil;
    self.rightTransparentImageView = nil;
    
    self.typeButton = nil;
    self.typeLabel = nil;
    self.titleImageView = nil;
    self.titleTextField = nil;
    self.dishNameButton = nil;
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [_leftButton setImage:[UIImage imageNamed:kLeftButtonImageFile]
                 forState:UIControlStateNormal];
    [_leftButton setImage:[UIImage imageNamed:kLeftButtonImageFile]
                 forState:UIControlStateHighlighted];
    [_leftButton addTarget:self action:@selector(leftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:_leftButton];
    
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0) {
        _rightButton.frame = CGRectMake(10, 0, 44, 44);
    }
    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateNormal];
    [_rightButton setImage:[UIImage imageNamed:kRightButtonImageFile]
                  forState:UIControlStateHighlighted];
    [_rightButton addTarget:self action:@selector(commitButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _rightButton.enabled = NO;
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_rightButton];
    
    self.navigationItem.titleView = [self createTwolineTitleView];
    
}

-(UIView *)createTwolineTitleView{
    UIView *titleView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]autorelease];
    UILabel *upperLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 150, 15)];
    upperLabel.backgroundColor = [UIColor clearColor];
    upperLabel.textColor = [UIColor whiteColor];
    upperLabel.textAlignment = UITextAlignmentCenter;
    upperLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    upperLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    upperLabel.shadowOffset = CGSizeMake(0, 0.5);
    upperLabel.text = @"上传图片";
    [titleView addSubview:upperLabel];
    [upperLabel release];
    
    UILabel *downLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 24, 150, 15)];
    downLabel.backgroundColor = [UIColor clearColor];
    downLabel.textColor = [UIColor whiteColor];
    downLabel.textAlignment = UITextAlignmentCenter;
    downLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    downLabel.shadowColor = [UIColor colorWithHex:0xff2c2c2];
    downLabel.shadowOffset = CGSizeMake(0, 0.5);
    downLabel.text = @"已保存到草稿";
    [titleView addSubview:downLabel];
    [downLabel release];
    return titleView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self requestData];
    
    _leftGreyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 21, 123, 121)];
    _leftGreyImageView.backgroundColor = [UIColor clearColor];
    [_leftGreyImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kGreyImageFile ofType:nil]]];
    [self.viewContainer addSubview:_leftGreyImageView];
    
    _leftCenterImageView = [[UIImageView alloc]initWithFrame:CGRectMake(56, 60, 41, 41)];
    _leftCenterImageView.backgroundColor = [UIColor clearColor];
    [_leftCenterImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kCenterImageFile ofType:nil]]];
    [self.viewContainer addSubview:_leftCenterImageView];
    
    
    _leftOrangeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 21, 123, 127)];
    _leftOrangeImageView.backgroundColor = [UIColor clearColor];
    [_leftOrangeImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kOrangeImageFile ofType:nil]]];
    _leftOrangeImageView.hidden = YES;
    [self.viewContainer addSubview:_leftOrangeImageView];
    
    
    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(18, 23, 118, 118)];
    _leftImageView.backgroundColor = [UIColor clearColor];
    [self.viewContainer addSubview:_leftImageView];
    
    _leftDeleteImageView = [[UIButton alloc]initWithFrame:CGRectMake(116, 9, 33, 33)];
    _leftDeleteImageView.backgroundColor = [UIColor clearColor];
    [_leftDeleteImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDeleteImageFile ofType:nil]] forState:UIControlStateNormal];
    _leftDeleteImageView.tag = 201;
    [_leftDeleteImageView addTarget:self action:@selector(deleteImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _leftDeleteImageView.hidden = YES;
    [self.viewContainer addSubview:_leftDeleteImageView];
    
    
    _leftTransparentImageView = [[UIButton alloc]initWithFrame:CGRectMake(16, 21, 123, 121)];
    _leftTransparentImageView.backgroundColor = [UIColor clearColor];
    _leftTransparentImageView.tag = 101;
    [_leftTransparentImageView addTarget:self action:@selector(transparentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewContainer addSubview:_leftTransparentImageView];
    
    
    //右边的button
    
    _rightGreyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(170, 21, 123, 121)];
    _rightGreyImageView.backgroundColor = [UIColor clearColor];
    [_rightGreyImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kGreyImageFile ofType:nil]]];
    _rightGreyImageView.hidden = YES;
    [self.viewContainer addSubview:_rightGreyImageView];
    
    _rightCenterImageView = [[UIImageView alloc]initWithFrame:CGRectMake(210, 60, 41, 41)];
    _rightCenterImageView.backgroundColor = [UIColor clearColor];
    [_rightCenterImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kCenterImageFile ofType:nil]]];
    _rightCenterImageView.hidden = YES;
    [self.viewContainer addSubview:_rightCenterImageView];
    
    
    _rightOrangeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(170, 21, 123, 127)];
    _rightOrangeImageView.backgroundColor = [UIColor clearColor];
    [_rightOrangeImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kOrangeImageFile ofType:nil]]];
    _rightOrangeImageView.hidden = YES;
    [self.viewContainer addSubview:_rightOrangeImageView];
    
    
    
    _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(172, 23, 118, 118)];
    _rightImageView.hidden = YES;
    _rightImageView.backgroundColor = [UIColor clearColor];
    [self.viewContainer addSubview:_rightImageView];
    
    _rightDeleteImageView = [[UIButton alloc]initWithFrame:CGRectMake(270, 9, 33, 33)];
    _rightDeleteImageView.backgroundColor = [UIColor clearColor];
    [_rightDeleteImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDeleteImageFile ofType:nil]] forState:UIControlStateNormal];
    [_rightDeleteImageView addTarget:self action:@selector(deleteImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _rightDeleteImageView.hidden = YES;
    [self.viewContainer addSubview:_rightDeleteImageView];
    
    _rightTransparentImageView = [[UIButton alloc]initWithFrame:CGRectMake(170, 21, 123, 121)];
    _rightTransparentImageView.backgroundColor = [UIColor clearColor];
    [_rightTransparentImageView addTarget:self action:@selector(transparentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _rightTransparentImageView.tag = 102;
    _rightTransparentImageView.hidden = YES;
    [self.viewContainer addSubview:_rightTransparentImageView];
    
    
    
    _titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 198, 292, 40)];
    UIImage *titleImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kFormImageFile ofType:nil]];
    titleImage = [titleImage resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    [_titleImageView setImage:titleImage];
    _titleImageView.userInteractionEnabled = YES;
    _titleImageView.hidden = YES;
    
    _titleTextField = [[UITextField alloc]initWithFrame:CGRectMake(14, 0, 200, 40)];
    _titleTextField.backgroundColor = [UIColor clearColor];
    _titleTextField.delegate = self;
    _titleTextField.textColor = [UIColor colorWithHex:0xff505050];
    _titleTextField.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _titleTextField.textAlignment = UITextAlignmentLeft;
    _titleTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _titleTextField.placeholder = @"图片标题...";
    [_titleTextField setReturnKeyType:UIReturnKeyDone];
    [_titleImageView addSubview:_titleTextField];
    
    _dishNameButton = [[UIButton alloc]initWithFrame:_titleImageView.bounds];
    _dishNameButton.backgroundColor = [UIColor clearColor];
    [_dishNameButton addTarget:self action:@selector(dishNameButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _dishNameButton.hidden = YES;
    [_titleImageView addSubview:_dishNameButton];
    
    [self.viewContainer addSubview:_titleImageView];
    
    _typeButton = [[UIButton alloc]initWithFrame:CGRectMake(14, 161, 294, 40)];
    _typeButton.backgroundColor = [UIColor clearColor];
    [_typeButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kOrderForm1ImageFile ofType:nil]] forState:UIControlStateNormal];
    [_typeButton addTarget:self action:@selector(typeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _typeButton.hidden = YES;
    
    _typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 12, 150, 16)];
    _typeLabel.backgroundColor = [UIColor clearColor];
    _typeLabel.textColor = [UIColor colorWithHex:0xff989797];
    _typeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _typeLabel.text = @"选择照片类型";
    [_typeButton addSubview:_typeLabel];
    
    [self.viewContainer addSubview:_typeButton];

    
    if (self.leftImage != nil) {
        UIImage *image = [self.leftImage copy];
        UIImage *cutImage = [UIImageView imageCutToResize:image :CGSizeMake(118, 118)];
        [_leftImageView setImage:cutImage];
        _leftOrangeImageView.hidden = NO;
        _leftDeleteImageView.hidden = NO;
        
        _rightGreyImageView.hidden = NO;
        _rightCenterImageView.hidden = NO;
        _rightImageView.hidden = NO;
        _rightTransparentImageView.hidden = NO;
        
        _typeButton.hidden = NO;
        _titleImageView.hidden = NO;
        
        _rightButton.enabled = YES;
        [image release];
    }
    if (self.rightImage != nil){
        UIImage *image = [self.rightImage copy];
        UIImage *cutImage = [UIImageView imageCutToResize:image :CGSizeMake(118, 118)];
        [_rightImageView setImage:cutImage];
        _rightOrangeImageView.hidden = NO;
        _rightDeleteImageView.hidden = NO;
        [image release];
    }
}

-(void)commitButtonClicked{
    if ([_typeLabel isEqual:@"选择照片类型"]) {
        [RCAppDelegate alertWithCustomizeString:@"请选择照片类型"];
        return;
    }
    if ([NSString stringIsNullOrEmpty:_titleTextField.text]) {
        [RCAppDelegate alertWithCustomizeString:@"请输入照片标题或选择菜名"];
        return;
    }
    _rightButton.enabled = NO;
    
    if (self.leftImage != nil && self.rightImage != nil) {
        NSData *imageData1 = nil;
        if (nil == UIImagePNGRepresentation(self.leftImage)) {
            imageData1 = UIImageJPEGRepresentation(self.leftImage, .6);
        }else{
            imageData1 = UIImagePNGRepresentation(self.leftImage);
        }
        
        NSData *imageData2 = nil;
        if (nil == UIImagePNGRepresentation(self.rightImage)) {
            imageData2 = UIImageJPEGRepresentation(self.rightImage, .6);
        }else{
            imageData2 = UIImagePNGRepresentation(self.rightImage);
        }
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        [dict setObject:_titleTextField.text forKey:@"filename1"];
        [dict setObject:self.typeParentID forKey:@"fileparentid1"];
        [dict setObject:self.typeID forKey:@"filetypeid1"];
        [dict setObject:self.typeLabel.text forKey:@"filetypename1"];
        [dict setObject:imageData1 forKey:@"uploadfile1"];
        
        [dict setObject:_titleTextField.text forKey:@"filename2"];
        [dict setObject:self.typeParentID forKey:@"fileparentid2"];
        [dict setObject:self.typeID forKey:@"filetypeid2"];
        [dict setObject:self.typeLabel.text forKey:@"filetypename2"];
        [dict setObject:imageData2 forKey:@"uploadfile2"];
        
        _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                                   TopHeight:0
                                            withBottomHeight:0
                                               withLeftWidth:0
                                              withRightWidth:0
                                        withExclusiveObjects:nil];
        _progressHub.delegate = self;
        _progressHub.labelText = @"上传中...";
        [self.view addSubview:_progressHub];
        [self.view bringSubviewToFront:_progressHub];
        [_progressHub show:YES];
        _uploadRequestID = [[IManager sharedInstance]requestUploadTwoPhotoFiles:self.merchantID withDicionary:dict withDelegate:self];
    }else if(self.leftImage != nil || self.rightImage != nil){
        
        NSData *imageData = nil;
        if (self.leftImage != nil) {
            if (nil == UIImagePNGRepresentation(self.leftImage)) {
                imageData = UIImageJPEGRepresentation(self.leftImage, .6);
            }else{
                imageData = UIImagePNGRepresentation(self.leftImage);
            }
        }else{
            if (nil == UIImagePNGRepresentation(self.rightImage)) {
                imageData = UIImageJPEGRepresentation(self.rightImage, .6);
            }else{
                imageData = UIImagePNGRepresentation(self.rightImage);
            }
        }
        
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        [dict setObject:_titleTextField.text forKey:@"filename1"];
        [dict setObject:self.typeParentID forKey:@"fileparentid1"];
        [dict setObject:self.typeID forKey:@"filetypeid1"];
        [dict setObject:self.typeLabel.text forKey:@"filetypename1"];
        [dict setObject:imageData forKey:@"uploadfile1"];
        
        _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                                   TopHeight:0
                                            withBottomHeight:0
                                               withLeftWidth:0
                                              withRightWidth:0
                                        withExclusiveObjects:nil];
        _progressHub.delegate = self;
        _progressHub.labelText = @"上传中...";
        [self.view addSubview:_progressHub];
        [self.view bringSubviewToFront:_progressHub];
        [_progressHub show:YES];
        _uploadRequestID = [[IManager sharedInstance]requestUploadOnePhotoFiles:self.merchantID withDicionary:dict withDelegate:self];
    }
    
    
    
}

#pragma TypeViewDelegate
-(void)typeButtonClicked{
    if (!_typeView) {
        [self refreshTypeView];
    }
    if (_typeView.hidden) {
        _typeView.hidden = NO;
    }else{
        _typeView.hidden = YES;
    }
    
}

-(void)dishNameButtonClicked{
    RCMerchantDishesViewController *vc = [[RCMerchantDishesViewController alloc]initWithMerchantID:self.merchantID withTypeID:self.typeID];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

#pragma dishViewControllerDelegate

-(void)selectDishedWithTypeName:(NSString *)name withTypeID:(NSString *)typeID{
    _titleTextField.text = name;
    self.typeParentID = self.typeID;
    if (nil == typeID) {
        self.typeID = @"";
    }else{
        self.typeID = typeID;
    }
}

-(void)selectToplistTypeItem:(RCMerchantPhotoUploadItem *)item{
    _typeLabel.text = item.itemName;
    _typeView.hidden = YES;
    self.typeID = item.itemID;
    self.typeParentID = item.itemParentID;
    
    if ([item.itemName isEqualToString:@"菜"]) {
        _titleTextField.placeholder = @"选菜名...";
        _titleTextField.text = @"";
        _dishNameButton.hidden = NO;
    }else{
        _dishNameButton.hidden = YES;
        _titleTextField.placeholder = @"图片标题...";
    }
}

#pragma -mark UITextFeildDelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma -mark Requst and response
-(void)requestData{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    _typeRequestID = [[IManager sharedInstance]requestUploadPhotoCateforyInfoWithoutType:self.merchantID withDelegate:self];
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_typeRequestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        RCMerchantPhotoUploadDataModel *typeDataModel = (RCMerchantPhotoUploadDataModel *)dataModel;
        self.typeDataModel = typeDataModel;
        [self refreshTypeView];
    }else if (_uploadRequestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        RCFeedbackDataModel *feedbackDataModel = (RCFeedbackDataModel *)dataModel;
        if (feedbackDataModel.errorNO == 0) {
            [RCAppDelegate alertWithCustomizeString:@"提交成功"];
            _rightButton.enabled = NO;
            [self performSelector: @selector(leftButtonClicked) withObject: nil afterDelay: 1.5f];
        }else{
            [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
            _rightButton.enabled = YES;
        }
    }
}

-(void)updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId{
    if (_typeRequestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        [RCAppDelegate alertWithCustomizeString:@"数据加载错误，请稍后重试"];
        [self.navigationController popViewControllerAnimated:YES];
    }else if (_uploadRequestID == requestId){
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        [RCAppDelegate alertWithCustomizeString:@"提交失败，请稍后重试"];
        _rightButton.enabled = YES;

    }
}


-(void)refreshTypeView{
    _typeView = [[RCMerchantUploadPhotoTypeView alloc]initWithFrame:CGRectMake(14, 201, 294, UIScreenHeight-64-201) andDataSource:self.typeDataModel];
    _typeView.delegate = self;
    [self.viewContainer addSubview:_typeView];
    _typeView.hidden = YES;
}

-(void)transparentButtonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    self.isLeftButtonClicked = NO;
    if (button.tag == 101) {
        self.isLeftButtonClicked = YES;
    }
    
    if (!_cameraManager){
        _cameraManager = [[NCCameraManager alloc] initWithParentController:self.navigationController isFromSelfView:NO];
        _cameraManager.delegate = (id<NCCameraManagerDelegate>)self;
    }
    
    UIActionSheet *actionSheet = nil;
    
    if ( [NCCameraManager supportGetPicFromAlbum] ) {
        
        if ( [NCCameraManager supportGetPicFromCamera] ) {
            actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromCameraString, kGetPicFromAlbumString, nil];
            
        } else {
            actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id<UIActionSheetDelegate>)self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:kGetPicFromAlbumString, nil];
            
        }
    }
    
    if (actionSheet) {
        [actionSheet addButtonWithTitle:kCancelString];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
        [actionSheet showFromTabBar:self.navigationController.tabBarController.tabBar];
        [actionSheet release];
    }

}

- (void)cancelCamera
{

}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if (buttonIndex != [actionSheet cancelButtonIndex]) {
		NSString *titleString = [actionSheet buttonTitleAtIndex:buttonIndex];
		if ( NSOrderedSame == [titleString caseInsensitiveCompare:kGetPicFromAlbumString]) {
            
            
            [_cameraManager selectExistingPicture];
            
            
		} else if ( NSOrderedSame == [titleString caseInsensitiveCompare:kGetPicFromCameraString]) {
            
            [_cameraManager getCameraPictureFromAlbum:NO];
            
		}
    }else {

    }
}

-(void)returnImage:(UIImage *)image{
    if (self.isLeftButtonClicked) {
        self.leftImage = image;
        UIImage *cutImage = [UIImageView imageCutToResize:image :CGSizeMake(118, 118)];
        [_leftImageView setImage:cutImage];
        _leftOrangeImageView.hidden = NO;
        _leftDeleteImageView.hidden = NO;
        
        _rightGreyImageView.hidden = NO;
        _rightCenterImageView.hidden = NO;
        _rightImageView.hidden = NO;
        _rightTransparentImageView.hidden = NO;
        
        _typeButton.hidden = NO;
        _titleImageView.hidden = NO;
        
        _rightButton.enabled = YES;
    }else{
        self.rightImage = image;
        UIImage *cutImage = [UIImageView imageCutToResize:image :CGSizeMake(118, 118)];
        [_rightImageView setImage:cutImage];
        _rightOrangeImageView.hidden = NO;
        _rightDeleteImageView.hidden = NO;
    }
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0){
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
}

- (void)returnError:(NSString*)errString
{

}

-(void)deleteImageButtonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    if (button.tag == 201) {
        [_leftImageView setImage:nil];
        _leftOrangeImageView.hidden = YES;
        _leftDeleteImageView.hidden = YES;
        self.leftImage = nil;
    }else{
        [_rightImageView setImage:nil];
        _rightOrangeImageView.hidden = YES;
        _rightDeleteImageView.hidden = YES;
        self.rightImage = nil;
    }
    
    if (self.leftImage == nil && self.rightImage == nil) {
        _rightButton.enabled = NO;
        
        _rightGreyImageView.hidden = YES;
        _rightCenterImageView.hidden = YES;
        _rightImageView.hidden = YES;
        _rightOrangeImageView.hidden = YES;
        _rightDeleteImageView.hidden = YES;
        _rightTransparentImageView.hidden = YES;
        _rightButton.enabled = NO;
    }
}

-(void)viewDidUnload{
    [super viewDidUnload];
    
    self.leftGreyImageView = nil;
    self.leftCenterImageView = nil;
    self.leftImageView = nil;
    self.leftOrangeImageView = nil;
    self.leftDeleteImageView = nil;
    self.leftTransparentImageView = nil;
    
    self.rightGreyImageView = nil;
    self.rightCenterImageView = nil;
    self.rightImageView = nil;
    self.rightOrangeImageView = nil;
    self.rightDeleteImageView = nil;
    self.rightTransparentImageView = nil;
}

-(void)leftButtonClicked{
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_typeRequestID];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_uploadRequestID];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
